package richbar.com.richbar.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import com.tencent.mm.opensdk.modelpay.PayReq;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.me.myassets.RepaidBalanceAc;
import richbar.com.richbar.activity.product.FirmOrderShopCartAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.OrderBean;
import richbar.com.richbar.bean.RechargeBean;
import richbar.com.richbar.bean.RepaidBalanceBean;
import richbar.com.richbar.help.Constants;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;


/**
 * Created by qyang on 2017-11-8.
 */

public class TipDialog {

    public static void show(Activity mActivity, String title, final OnListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        View view = View.inflate(mActivity, R.layout.dialog_tip, null);
        builder.setView(view);
        builder.setCancelable(false);
        TextView titleTv = view.findViewById(R.id.title);
        titleTv.setText(title);
        TextView besureTv = view.findViewById(R.id.i_know);
        final AlertDialog dialog = builder.create();
        besureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onBesure();
                dialog.cancel();
            }
        });
        dialog.show();
    }

    public static void showpay(final Activity mActivity) {
        final LoadingDialog dialogs = new LoadingDialog(mActivity, 0);
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        View view = View.inflate(mActivity, R.layout.dialog_pay, null);
        builder.setView(view);
        builder.setCancelable(false);
        final EditText edit = view.findViewById(R.id.edit);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        TextView tv_sure = view.findViewById(R.id.tv_sure);
        final AlertDialog dialog = builder.create();
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogs.show();
                NetWorkUtil.startRecharge("1", edit.getText().toString().trim(), new HttpUtils.HttpCallback() {
                    @Override
                    public void onSuccess(String data) {
                        dialogs.dismiss();
                        final RechargeBean rechargeBean = MyGson.getInstance().fromJson(data, RechargeBean.class);
                        if (rechargeBean.getCode() == 200) {
                            sendPayRequest(rechargeBean, mActivity);
                            FirmOrderShopCartAc.bln_kind = false;
                        } else {
                            ToastUtil.showToast(mActivity, rechargeBean.getMsg());
                        }

                    }

                    @Override
                    public void onError(String msg) {
                        super.onError(msg);
                        dialogs.dismiss();
                        ToastUtil.showToast(mActivity, msg);
                    }
                });
                dialog.cancel();
            }
        });
        dialog.show();
    }



    public interface OnListener {
        void onBesure();
    }

    /**
     * 调用微信支付
     */
    public static void sendPayRequest(RechargeBean rechargeBean, Activity activity) {
        try {
            PayReq req = new PayReq();
            req.appId = Constants.APP_ID;
            req.partnerId = rechargeBean.getData().getPayOption().getPartnerid();
            req.prepayId = rechargeBean.getData().getPayOption().getPrepayid();
            req.nonceStr = rechargeBean.getData().getPayOption().getNoncestr();
            req.timeStamp = rechargeBean.getData().getPayOption().getTimestamp();
            req.packageValue = rechargeBean.getData().getPayOption().getPackageX();
            req.sign = rechargeBean.getData().getPayOption().getSign();
            req.extData = "app data"; // optional
            Toast.makeText(activity, "调起微信支付", Toast.LENGTH_SHORT).show();
            // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
            BaseApplication.iwxapi.sendReq(req);

        } catch (Exception e) {
            Log.e("PAY_GET", "异常：" + e.getMessage());
            Toast.makeText(activity, "异常：" + e.getMessage(), Toast.LENGTH_SHORT).show();

        }
    }
}

